#!/usr/bin/env python
#
#
#  MP testing with Pool/Queue


from multiprocessing import Queue, Pool, Process

import time

import random

def do_stuff(x):
    time.sleep(3)
    print "     ---> %s complete" % x
    

def main():
    n = 0

    # Initialize Queue
    q = Queue()
    
    # Setting up Pool with max number of processes
    p = Pool(processes=2)
    
    while True:
        # Increment iteration counter
        n = n + 1
        
        # Setting up Pool with max number of processes
        #p = Pool(processes=2)
        
        print "======= Rotation " + str(n) + "======="
        print "Queue size before add-in: ", q.qsize()

        # Adding items to queue - only populate on every 4th rotation
        if n in range(1, 100, 4):
            for e, item in enumerate(range(1,8,1)):
                q.put(str(n) + "-" + str(item))
        
        # Sleep --- Queue is being picky
        time.sleep(0.25)
    
        print "Queue size before map_async: ", q.qsize()
    
        print "b4 async map"
        results = p.map_async(do_stuff, [ q.get_nowait() for item in range(1, q.qsize() + 1, 1) ])
        print "after async map"
    
        # Show results --- we really don't need this
        #print "Results: ", results.get()
    
        # How big is our queue?
        print "Queue size after map_async: ", q.qsize()
        print "========================================"
        print
        
        # Simulate tailing queue blocking 
        time.sleep(1)

if __name__ == '__main__':
    main()